﻿using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class YCACOFS : DocumentBase
    {
        #region-------------Properties-----------------------
        ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        INTEXBL INTEXBL
        {
            get
            {
                return app.INTEXBL;
            }
        }
        INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }
        }
        bool IsLoaded = false;
        DataRow LastRow = null;

        YCACOFSData m_Data;
        DataTable tbUser = null;
        public SETINTEXDB SetEnv
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetEnv");
            }
        }
        public SETINTEXDB SetData
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetData");
            }
        }
        public SETINTEXDB SetProd
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetProd");
            }
        }
        CollectionViewSource vwABPOS_MAT
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwABPOS_MAT");
            }
        }
        CollectionViewSource vwEKPOS
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwEKPOS");
            }
        }
        CollectionViewSource vwABCO
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwABCO");
            }
        }
        DataRow dwABCO
        {
            get
            {
                if (vwABCO.View == null)
                    return null;
                if ((DataRowView)vwABCO.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwABCO.View.CurrentItem).Row;
            }
        }
        CollectionViewSource vwPABCO
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwPABCO");
            }
        }
        DataRow dwPABCO
        {
            get
            {
                if (vwPABCO.View == null)
                    return null;
                if ((DataRowView)vwPABCO.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwPABCO.View.CurrentItem).Row;
            }
        }
        CollectionViewSource vwABPOS
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwABPOS");
            }
        }
        DataRow dwABPOS
        {
            get
            {
                if (vwABPOS.View == null)
                    return null;
                if ((DataRowView)vwABPOS.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwABPOS.View.CurrentItem).Row;
            }
        }
        CollectionViewSource vwFDECKMAT
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwFDECKMAT");
            }
        }
        DataRow dwFDECKMAT
        {
            get
            {
                if (vwFDECKMAT.View == null)
                    return null;
                if ((DataRowView)vwFDECKMAT.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwFDECKMAT.View.CurrentItem).Row;
            }
        }
        CollectionViewSource vwWDECKMAT
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwWDECKMAT");
            }
        }
        DataRow dwWDECKMAT
        {
            get
            {
                if (vwWDECKMAT.View == null)
                    return null;
                if ((DataRowView)vwWDECKMAT.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwWDECKMAT.View.CurrentItem).Row;
            }
        }
        CollectionViewSource vwXDECK
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwXDECK");
            }
        }
        DataRow dwXDECK
        {
            get
            {
                if (vwXDECK.View == null)
                    return null;
                if ((DataRowView)vwXDECK.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwXDECK.View.CurrentItem).Row;
            }
        }


        string ART
        {
            get
            {
                return m_Data.Left(txtFSART.Text.ToString() + "                      ", 20);
            }
        }
        BindingListCollectionView cvXDECK
        {
            get
            {
                return (BindingListCollectionView)CollectionViewSource.GetDefaultView(dlgXDECK.ItemsSource);
            }
        }
        BindingListCollectionView cvABCO
        {
            get
            {
                return (BindingListCollectionView)CollectionViewSource.GetDefaultView(dlgABCO.ItemsSource);
            }
        }
        ITXEXRPT ITXEXRPT;
        string user = "";
        #endregion
        public YCACOFS()
        {
            InitializeComponent();
            this.Name = "生产成本";
        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            ITXEXRPT = new ITXEXRPT();
            ITXEXRPT.Root = "C:\\IntexReport";
            ITXEXRPT.strCompany = app.company;
            ITXEXRPT.PrintType = "OpenExcel";
            tbUser = (DataTable)dicParamater["USERRIGHT"];
            m_Data = new YCACOFSData();
            user = (string)dicParamater["User"];

            ReSetEnv();
        }
        public void ReSetEnv()
        {
            DataSet setResult = new DataSet();
            try
            {
                setResult = m_Data.LoadEnv();
                SetEnv.Clear();
                SetEnv.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                SetEnv.Clear();
                SetEnv.Merge(setResult);
            }
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (IsLoaded)
                    return;
                IsLoaded = true;
                vwABCO.View.CurrentChanged += vwABCO_CurrentChanged;
                vwWDECKMAT.View.CurrentChanged += vwWDECKMAT_CurrentChanged;
                vwFDECKMAT.View.CurrentChanged += vwFDECKMAT_CurrentChanged;
                vwXDECK.View.CurrentChanged += vwXDECK_CurrentChanged;
                if (app.User.Trim() == "YCAV")
                {
                    txtFSART.Text = "24310090410";
                }
                DataRow dwData = SetData.FSSTAMM.NewRow();
                dwData["FSMANDANT"] = "00";
                DataSet SResult = INTEXDBC.QueryFSSTAMM(dwData, 7);
                foreach (DataRow row in SResult.Tables[0].Rows)
                {
                    txtYear.Text = row["FSKOLL"].ToString();
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return;

        }

        void vwXDECK_CurrentChanged(object sender, EventArgs e)
        {
            zoneMaterial.IsExpanded = true;
            zoneFabric.IsExpanded = false;
            if (dwXDECK == null)
                return;
            txtMART.Text = dwXDECK["ABAART"].ToString();
        }

        void vwWDECKMAT_CurrentChanged(object sender, EventArgs e)
        {

            if (dwWDECKMAT == null)
                return;
            if (LastRow == dwWDECKMAT)
                return;
            LastRow = dwWDECKMAT;
            try
            {
                zoneMaterial.IsExpanded = false;
                zoneFabric.IsExpanded = true;

                txtMTYPE.Text = "F";
                txtMART.Text = dwWDECKMAT["ART"].ToString();
                txtMCOLOR.Text = dwWDECKMAT["FARBE"].ToString();
                txtMSIZE.Text = "       ";
                btnFilterMat_Click(null, null);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        void vwFDECKMAT_CurrentChanged(object sender, EventArgs e)
        {

            if (dwFDECKMAT == null)
                return;
            if (LastRow == dwFDECKMAT)
                return;
            LastRow = dwFDECKMAT;
            try
            {
                zoneFabric.IsExpanded = false;
                zoneMaterial.IsExpanded = true;

                txtMTYPE.Text = "T";
                txtMART.Text = dwFDECKMAT["ART"].ToString();
                txtMCOLOR.Text = dwFDECKMAT["FARBE"].ToString();
                txtMSIZE.Text = dwFDECKMAT["SIZE"].ToString();
                btnFilterMat_Click(null, null);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        void vwABCO_CurrentChanged(object sender, EventArgs e)
        {
            if (dwABCO == null)
                return;
            try
            {

                DataSet SResult = new DataSet();
                cvXDECK.CustomFilter = null;
                txtFSART.Text = dwABCO["ART"].ToString();
                using (new LongOperation(this, "数据更新中"))
                {
                    string PROGRPD = dwABCO["PROGRPD"].ToString();
                    if (PROGRPD.Trim() == "")
                    {
                        dwABCO["PROGRPD"] = Helpers.Helper.GetSDDAT(SetEnv, "0196", dwABCO["PROGRP"].ToString(), "V");

                    }
                    string ARTGRPD = dwABCO["ARTGRPD"].ToString();
                    if (ARTGRPD.Trim() == "")
                    {
                        dwABCO["ARTGRPD"] = Helpers.Helper.GetSDDAT(SetEnv, "0178", dwABCO["ARTGRP"].ToString(), "V");
                    }
                    string MATARTD = dwABCO["MATARTD"].ToString();
                    if (MATARTD.Trim() == "")
                    {
                        dwABCO["MATARTD"] = Helpers.Helper.GetSDDAT(SetEnv, "0649", dwABCO["MATART"].ToString(), "V");
                    }
                    string MATGRPD = dwABCO["MATGRPD"].ToString();
                    if (MATGRPD.Trim() == "")
                    {
                        dwABCO["MATGRPD"] = Helpers.Helper.GetSDDAT(SetEnv, "0312", dwABCO["MATGRP"].ToString(), "V");
                    }
                    LoadDetail(dwABCO);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                if (vwPABCO.View != null)
                {
                    vwPABCO.View.MoveCurrentToFirst();
                }
            }
        }
        void LoadDetail(DataRow _dwABCO)
        {
            using (new LongOperation(this, "数据更新中"))
            {
                SetProd.Clear();
                string filter = "KRS='" + dwABCO["KRS"].ToString() + "' AND LFD='" + dwABCO["LFD"].ToString() + "'";
                DataTable tb = SetData.Tables["ICC_ABPOS_MCO"].Select(filter).CopyToDataTable();
                tb.TableName = "ICC_ABPOS_MCO";
                SetProd.Merge(tb);
                DataSet SResult = m_Data.LoadDetail(SetProd, _dwABCO);
                SetProd.Merge(SResult);
            }
        }
        private void btnRefresh_Click(object sender, EventArgs e)
        {
            DataSet SResult = new DataSet();
            if (dwABCO == null)
            {
                MessageBox.Show("请选择一款进行刷新");
                return;
            }
            try
            {//FSMANDANT	FSART	FSVAR
                string filter = "KRS='" + dwABCO["KRS"].ToString() + "' AND LFD='" + dwABCO["LFD"].ToString() + "'";
                cvABCO.CustomFilter = filter;
                m_Data.UpdateProdCost(dwABCO);
                m_Data.UpdateMatCost(dwABCO);
                btnLoad_Click(null, null);
                vwABCO.View.MoveCurrentTo(-1);
                if (vwABCO.View != null)
                {
                    vwABCO.View.MoveCurrentToFirst();
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnPrint_Click(object sender, EventArgs e)
        {
            ITXEXRPT.dsSource = SetProd;
            ITXEXRPT.dsSource.Merge(SetEnv);
            ITXEXRPT.DocType = "YCACOFS";
            ITXEXRPT.strExcelFile = @"YCA\YCACOFS.xls";
            string filename = ITXEXRPT.WriteExcelReport();
        }

        private void RefreshXDECK()
        {
            try
            {
                if (dwXDECK == null)
                    return;
                DataTable tbXDECK = SetProd.ICC_ABAUFM;
                double QTYSTD = 0;
                double QTYPLAN = 0;
                double TQTYACT = 0;
                double QTYACT = 0;
                double PRSTD = 0;
                double PREK = 0;
                double PRPLAN = 0;
                var qwd = from DataRow XD in tbXDECK.AsEnumerable()
                          where XD["ABAVKAUFKRS"].ToString() == dwXDECK["ABAVKAUFKRS"].ToString()
                          && XD["ABAVKAUFLFD"].ToString() == dwXDECK["ABAVKAUFLFD"].ToString()
                          && XD["ABAART"].ToString() == dwXDECK["ABAART"].ToString()
                          && XD["ABAVAR"].ToString() == dwXDECK["ABAVAR"].ToString()
                          && XD["ABAFARBE"].ToString() == dwXDECK["ABAFARBE"].ToString()
                          && XD["ABAGROE"].ToString() == dwXDECK["ABAGROE"].ToString()
                          select XD;
                foreach (DataRow XD in qwd)
                {
                    if (XD["QTYSTD"].ToString().Trim() == "")
                    {
                        XD["QTYSTD"] = "0";
                    }
                    if (XD["QTYPLAN"].ToString().Trim() == "")
                    {
                        XD["QTYPLAN"] = "0";
                    }
                    if (XD["QTYACT"].ToString().Trim() == "")
                    {
                        XD["QTYACT"] = "0";
                    }
                    if (XD["PREK"].ToString().Trim() == "")
                    {
                        XD["PREK"] = "0";
                    }

                    if (XD["PRPLAN"].ToString().Trim() == "")
                    {
                        XD["PRPLAN"] = "0";
                    }
                    if (XD["PRSTD"].ToString().Trim() == "")
                    {
                        XD["PRSTD"] = "0";
                    }
                    if (Convert.ToDouble(XD["PRSTD"].ToString().Trim()) == 0)
                    {
                        XD["PRSTD"] = XD["PREK"];
                    }
                    if (Convert.ToDouble(XD["PRPLAN"].ToString().Trim()) == 0)
                    {
                        XD["PRPLAN"] = XD["PREK"];
                    }

                    double RT = 1;
                    /*if(XD["WAUEBM"].ToString().Trim()=="KG")
                    {
                        RT = Convert.ToDecimal(XD["WAFGRLFD"].ToString()) * Convert.ToDecimal(0.001);
                    }*/
                    QTYSTD = System.Convert.ToDouble(XD["QTYSTD"].ToString()) * RT;
                    QTYPLAN = System.Convert.ToDouble(XD["QTYPLAN"].ToString()) * RT;
                    QTYACT = System.Convert.ToDouble(XD["QTYACT"].ToString()) * RT;
                    TQTYACT += QTYACT;
                    PRSTD = System.Convert.ToDouble(XD["PRSTD"].ToString());
                    PREK = System.Convert.ToDouble(XD["PREK"].ToString());
                    PRPLAN = System.Convert.ToDouble(XD["PRPLAN"].ToString());
                    XD["VQTYSTDPRSTD"] = Helpers.Helper.RoundX(QTYSTD * PRSTD, 100);
                    XD["VQTYSTDPREK"] = Helpers.Helper.RoundX(QTYSTD * PREK, 100);
                    XD["VQTYSTDPRPLAN"] = Helpers.Helper.RoundX(QTYSTD * PRPLAN, 100);
                    XD["VQTYPLANPRSTD"] = Helpers.Helper.RoundX(QTYPLAN * PRSTD, 100);
                    XD["VQTYPLANPREK"] = Helpers.Helper.RoundX(QTYPLAN * PREK, 100);
                    XD["VQTYPLANPRPLAN"] = Helpers.Helper.RoundX(QTYPLAN * PRPLAN, 100);
                    XD["VQTYACTPRSTD"] = Helpers.Helper.RoundX(QTYACT * PRSTD, 100);
                    XD["VQTYACTPREK"] = Helpers.Helper.RoundX(QTYACT * PREK, 100);
                    XD["VQTYACTPRPLAN"] = Helpers.Helper.RoundX(QTYACT * PRPLAN, 100);
                }

                //当前dwXDECK所对应的生产单
                //将生产单中的款，按款号，颜色对dwXDECK的物料进行数量汇总，更新到生产单的原料价值
                /*
                 select cm.krs,cm.lfd,cm.art,cm.var,cm.farbe,sum(xd.vqtystdprstd)
                 * from icc_xdeck XD, icc_abpos_mco CM,abpos ab
                 * where xd.krs=cm.krs 
                 * and xd.lfd=cm.lfd
                 * and cm.krs=ab.abpaufkrs
                 * and cm.lfd=ab.abpauflfd
                 * and cm.farbe=ab.abpfarbe
                 * and xd.pos=ab.abpaufpos
                 * --and xd.lfd='000143' and xd.art like 'BG41007%'
                 * group by cm.krs,cm.lfd,cm.art,cm.var,cm.farbe;
                 */
                var query = from XD in tbXDECK.AsEnumerable()
                            from CM in SetProd.ICC_ABPOS_MCO.AsEnumerable()
                            from AB in SetProd.ABPOS.AsEnumerable()
                            where XD["ABAVKAUFKRS"].ToString() == CM["KRS"].ToString()
                            && XD["ABAVKAUFLFD"].ToString() == CM["LFD"].ToString()
                            && CM["KRS"].ToString() == AB["ABPAUFKRS"].ToString()
                            && CM["LFD"].ToString() == AB["ABPAUFLFD"].ToString()
                           && CM["ART"].ToString() == AB["ABPART"].ToString()
                              && CM["VAR"].ToString().Trim() == AB["ABPVAR"].ToString().Trim()
                             && CM["FARBE"].ToString() == AB["ABPFARBE"].ToString()
                                && XD["ABAVKAUFKRS"].ToString() == dwXDECK["ABAVKAUFKRS"].ToString()
                                  && XD["ABAVKAUFLFD"].ToString() == dwXDECK["ABAVKAUFLFD"].ToString()
                                && XD["ABAVKAUFPOS"].ToString() == AB["ABPAUFPOS"].ToString()
                            group XD by new
                            {
                                KRS = CM["KRS"].ToString(),
                                LFD = CM["LFD"].ToString(),
                                ART = CM["ART"].ToString(),
                                VAR = CM["VAR"].ToString().Trim(),
                                FARBE = CM["FARBE"].ToString(),
                            }
                                into g
                                select new
                                {
                                    KEY = g.Key,
                                    VMQSTDPSTD = g.Sum(XD => XD.Field<Decimal>("VQTYSTDPRSTD")),
                                    VMQSTDPEK = g.Sum(XD => XD.Field<Decimal>("VQTYSTDPREK")),
                                    VMQSTDPPLAN = g.Sum(XD => XD.Field<Decimal>("VQTYSTDPRPLAN")),

                                    VMQPLANPSTD = g.Sum(XD => XD.Field<Decimal>("VQTYPLANPRSTD")),
                                    VMQPLANPEK = g.Sum(XD => XD.Field<Decimal>("VQTYPLANPREK")),
                                    VMQPLANPPLAN = g.Sum(XD => XD.Field<Decimal>("VQTYPLANPRPLAN")),

                                    VMQACTPSTD = g.Sum(XD => XD.Field<Decimal>("VQTYACTPRSTD")),
                                    VMQACTPEK = g.Sum(XD => XD.Field<Decimal>("VQTYACTPREK")),
                                    VMQACTPPLAN = g.Sum(XD => XD.Field<Decimal>("VQTYACTPRPLAN")),
                                };

                foreach (var XD in query)
                {
                    var qm = from DataRow rm in SetProd.ICC_ABPOS_MCO.AsEnumerable()
                             where rm["KRS"].ToString() == XD.KEY.KRS
                             && rm["LFD"].ToString() == XD.KEY.LFD
                             && rm["ART"].ToString() == XD.KEY.ART
                             && rm["VAR"].ToString().Trim() == XD.KEY.VAR.Trim()
                             && rm["FARBE"].ToString() == XD.KEY.FARBE
                             select rm;
                    foreach (DataRow rm in qm)
                    {
                        rm["VMQSTDPSTD"] = XD.VMQSTDPSTD;
                        rm["VMQSTDPEK"] = XD.VMQSTDPEK;
                        rm["VMQSTDPPLAN"] = XD.VMQSTDPPLAN;

                        rm["VMQPLANPSTD"] = XD.VMQPLANPSTD;
                        rm["VMQPLANPEK"] = XD.VMQPLANPEK;
                        rm["VMQPLANPPLAN"] = XD.VMQPLANPPLAN;

                        rm["VMQACTPSTD"] = XD.VMQACTPSTD;
                        rm["VMQACTPEK"] = XD.VMQACTPEK;
                        rm["VMQACTPPLAN"] = XD.VMQACTPPLAN;
                        rm["PRODV"] = Helpers.Helper.RoundX(Convert.ToDouble(rm["MPRICE"].ToString()) * Convert.ToDouble(rm["QTY"].ToString()), 1);
                    }
                }
                if (txtMTYPE.Text == "T")
                {

                    dwFDECKMAT["QTYACT"] = Helpers.Helper.RoundX(TQTYACT, 1);

                }
                if (txtMTYPE.Text == "F")
                {
                    dwWDECKMAT["QTYACT"] =Helpers.Helper.RoundX(TQTYACT,1);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                if (vwXDECK.View != null)
                    vwXDECK.View.MoveCurrentToFirst();
            }
        }
        private void btnApprove_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (dwPABCO == null)
                    return;
                if (dwXDECK == null)
                    return;
                var qxd = from DataRow rx in SetProd.ICC_ABAUFM.AsEnumerable()
                          where rx["MTYPE"].ToString() == dwXDECK["MTYPE"].ToString()
                          && rx["ABAART"].ToString() == dwXDECK["ABAART"].ToString()
                          && rx["ABAVAR"].ToString() == dwXDECK["ABAVAR"].ToString()
                          && rx["ABAFARBE"].ToString() == dwXDECK["ABAFARBE"].ToString()
                          && rx["ABAGROE"].ToString() == dwXDECK["ABAGROE"].ToString()
                          select rx;
                foreach (DataRow row in qxd)
                {
                    row["CONTROL"] = "FN ";
                    double RT = 1;
                    if (row["WAUEBM"].ToString().Trim() == "KG")
                    {
                        RT = Convert.ToDouble(row["WAFGRLFD"].ToString()) * 0.001;
                    }
                    double QTYACT = System.Convert.ToDouble(row["QTYACT"].ToString());
                    row["QTYACT"] = Math.Round(QTYACT / RT, 2);
                    INTEXDBC.UpdateICC_ABAUFM(row, 99);
                    row["QTYACT"] = QTYACT;
                }
                var qab = (from DataRow rx in SetProd.ICC_ABAUFM.AsEnumerable()
                           select rx).Min(XD => XD.Field<string>("CONTROL"));
                string control = (string)qab;
                foreach (DataRow rx in SetProd.ICC_ABPOS_MCO.Rows)
                {
                    rx["CONTROL"] = control;
                    INTEXDBC.UpdateICC_ABPOS_MCO(rx, 1);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {

            }
        }
        private void btnPushQty_Click(object sender, RoutedEventArgs e)
        {

            try
            {
                if (dwXDECK == null)
                    return;
                if (txtNQTy.Text.Trim() == "")
                    txtNQTy.Text = "0";
                string krs = dwXDECK["ABAVKAUFKRS"].ToString();
                string lfd = dwXDECK["ABAVKAUFLFD"].ToString();
                string art = dwXDECK["ABAART"].ToString();
                string var = dwXDECK["ABAVAR"].ToString();
                string color = dwXDECK["ABAFARBE"].ToString();
                string size = dwXDECK["ABAGROE"].ToString();
                double TQTY = 0, newqty = 0;
                string szQty = "0";
                DataTable tbXDECK = SetProd.Tables["ICC_ABAUFM"];
                newqty = System.Convert.ToDouble(txtNQTy.Text.ToString());
                var query = from XD in tbXDECK.AsEnumerable()
                            where XD["ABAVKAUFKRS"].ToString() == krs
                               && XD["ABAVKAUFLFD"].ToString() == lfd
                               && XD["ABAART"].ToString().Trim() == art.Trim()
                               && XD["ABAVAR"].ToString().Trim() == var.Trim()
                               && XD["ABAFARBE"].ToString().Trim() == color.Trim()
                               && XD["ABAGROE"].ToString().Trim() == size.Trim()
                            group XD by new
                            {
                                ART = XD["ABAART"].ToString(),
                                VAR = XD["ABAVAR"].ToString(),
                                FARBE = XD["ABAFARBE"].ToString(),
                                SIZE = XD["ABAGROE"].ToString(),
                            }
                                into g
                                select new
                                {
                                    KEY = g.Key,
                                    QTYSTD = g.Sum(XD => Convert.ToDouble(XD.Field<Decimal>("QTYSTD"))),
                                };

                foreach (var XD in query)
                {
                    TQTY = XD.QTYSTD;
                }
                if (TQTY == 0)
                    return;
                var qx = from DataRow row in SetProd.ICC_ABAUFM.AsEnumerable()
                         where row["ABAART"].ToString().Trim() == art.Trim()
                         && row["ABAVAR"].ToString().Trim() == var.Trim()
                         && row["ABAFARBE"].ToString().Trim() == color.Trim()
                         && row["ABAGROE"].ToString().Trim() == size.Trim()
                         select row;
                foreach (DataRow row in qx)
                {
                    double QTYSTD = System.Convert.ToDouble(row["QTYSTD"].ToString());
                    double QTYACT = System.Convert.ToDouble(row["QTYACT"].ToString());
                    double RT = 1;
                    if (row["WAUEBM"].ToString().Trim() == "KG")
                    {
                        //RT = Convert.ToDecimal(row["WAFGRLFD"].ToString()) * Convert.ToDecimal(0.001);                     
                    }
                    row["QTYACT"] = System.Math.Round(newqty * RT * (QTYSTD / TQTY), 2);//QTYACT+ newqty * (QTYSTD / TQTY);
                }
                //SetProd.AcceptChanges();
                RefreshXDECK();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);

            }
            finally
            {
                if (vwXDECK.View != null)
                    vwXDECK.View.MoveCurrentToFirst();
            }
        }

        private void btnFilterMat_Click(object sender, RoutedEventArgs e)
        {
            string filter = "MTYPE LIKE '" + txtMTYPE.Text.ToString() + "%' AND ABAART LIKE '%" + txtMART.Text.ToString().Trim()
                + "%' AND ABAFARBE LIKE '" + txtMCOLOR.Text.ToString().Trim() + "%' AND ABAGROE LIKE '" + txtMSIZE.Text.ToString().Trim() + "%'";
            cvXDECK.CustomFilter = filter;
            if (vwXDECK.View != null)
                vwXDECK.View.MoveCurrentToFirst();
        }
        private void btnFilterMatClear_Click(object sender, RoutedEventArgs e)
        {
            cvXDECK.CustomFilter = null;
        }
        private void btnFilterABCO_Click(object sender, RoutedEventArgs e)
        {
            string filter = "ART LIKE '%" + txtFSART.Text.ToString().Trim() + "%'";
            cvABCO.CustomFilter = filter;
        }
        private void btnFilterABCOClear_Click(object sender, RoutedEventArgs e)
        {
            cvABCO.CustomFilter = null;
        }
        private void cbSECTION_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBox cb = sender as ComboBox;
            txtSection.Text += cb.SelectedValue + ";";
        }

        private void btnLoad_Click(object sender, RoutedEventArgs e)
        {
            DataSet SResult = new DataSet();
            SetProd.Clear();
            try
            {//FSMANDANT	FSART	FSVAR
                DataRow dwInput = null;
                using (new LongOperation(this, "数据更新中"))
                {
                    dwInput = SetProd.ICC_ABPOS_MCO.NewRow();
                    //dwInput["MANDANT"] = "00";
                    dwInput["ART"] = ART;
                    SResult = m_Data.Load(dwInput, txtSection.Text);
                    SetData.Clear();
                    SetData.Merge(SResult);
                    if (vwABCO.View != null)
                    {
                        vwABCO.View.MoveCurrentToFirst();
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void btnReset_Click(object sender, RoutedEventArgs e)
        {
            INTEXDBC.UpdateICC_ABAUFM(dwABCO, 55);
        } 
    }
}
